package com.beyondsoft.dtc.mobile.automation.model;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.beyondsoft.dtc.mobile.automation.base.GlobalConstants;
import org.apache.commons.codec.digest.DigestUtils;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

/**
 * Created by Robin on 1/28/2016.
 */
public class TaskResult {

	@Expose
	@SerializedName("task_id")
	private String taskId = "";

	@Expose
	@SerializedName("device_id")
	private String deviceId = "";

	@Expose
	@SerializedName("app_name")
	private String appName = "";

	@Expose
	@SerializedName("app_icon_path")
	private String appIcon = "";

	@Expose
	@SerializedName("app_size")
	private long appSize = 0;

	@Expose
	@SerializedName("app_download")
	private int appDownload = 0;

	@Expose
	@SerializedName("app_parse")
	private int appParse = 0;

	@Expose
	@SerializedName("script_parse")
	private int scriptParse = 0;

	@Expose
	@SerializedName("app_install")
	private int appInstall = 0;

	@Expose
	@SerializedName("app_start")
	private int appStart = 0;

	@Expose
	@SerializedName("app_run")
	private int appRun = 1;

	@Expose
	@SerializedName("app_run_error_desc")
	private String appRunErrorDesc = "";

	@Expose
	@SerializedName("app_uninstall")
	private int appUninstall = 0;

	@Expose
	@SerializedName("start_time")
	private long startTime = 0;

	@Expose
	@SerializedName("start_duration")
	private int startDuration = 0;

	@Expose
	@SerializedName("install_duration")
	private int installDuration = 0;

	@Expose
	@SerializedName("cpu_usage")
	private List<String> cpuUsage = new ArrayList<String>();

	@Expose
	@SerializedName("memory_usage")
	private List<String> memoryUsage = new ArrayList<String>();

	@Expose
	@SerializedName("traffic_up")
	private String trafficUp = "0";

	@Expose
	@SerializedName("traffic_down")
	private String trafficDown = "0";

	@Expose
	@SerializedName("log_file")
	private String appRunLog = "";

	@Expose
	@SerializedName("log_file_error")
	private String appRunErrorLog = "";

	@Expose
	@SerializedName("log_file_install")
	private String appInstallLog = "";

	@Expose
	@SerializedName("log_file_uninstall")
	private String appUninstallLog = "";

	@Expose
	@SerializedName("screenshot")
	private List<String> screenshots = new ArrayList<String>();

	@Expose
	@SerializedName("sign")
	private String sign = "";

	@Expose
	@SerializedName("task_type")
	private String taskType = "";

	@Expose
	@SerializedName("mail")
	private String mail;

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	// //测试开始时间
	// @Expose
	// @SerializedName("test_start_time")
	// private long testStartTime = 0;
	//

	// //测试结束时间
	// @Expose
	// @SerializedName("test_end_time")
	// private long testEndTime = 0;
	//
	// //测试时间间隔
	// @Expose
	// @SerializedName("test_duration")
	// private Long testDuration = 0L;

	// 上下行使用流量
	// @Expose
	// @SerializedName("traffic_usage")
	// private Map<String,String> trafficUsage =new IdentityHashMap<String,
	// String>();

	@Expose
	@SerializedName("traffic_up_usage")
	private List<String> traffic_up_usage = new ArrayList<String>();

	@Expose
	@SerializedName("traffic_down_usage")
	private List<String> traffic_down_usage = new ArrayList<String>();

	@Expose
	@SerializedName("temperature_arr")
	private List<String> temperature_arr = new ArrayList<String>();

	@Expose
	@SerializedName("fps_arr")
	private List<String> fps_arr = new ArrayList<String>();

	@Expose
	@SerializedName("test_time")
	private List<String> test_time = new ArrayList<String>();

	@Expose
	@SerializedName("install_error_reason")
	private String install_error_reason;

	@Expose
	@SerializedName("start_error_reason")
	private String start_error_reason;

	@Expose
	@SerializedName("uninstall_error_reason")
	private String uninstall_error_reason;

	public String getSignCode(String key) {
		String rawSign = new StringBuilder().append(appDownload)
				.append(appInstall).append(appName).append(appParse)
				.append(appRun).append(appSize).append(appStart)
				.append(appUninstall).append(deviceId).append(startDuration)
				.append(startTime).append(taskId).append(key).toString();
		try {
			// MD5 encode
			sign = DigestUtils.md5Hex(rawSign
					.getBytes(GlobalConstants.DEFAULT_ENCODE));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return sign;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getDeviceId() {
		return deviceId;
	}

	public void setDeviceId(String deviceId) {
		this.deviceId = deviceId;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getAppIcon() {
		return appIcon;
	}

	public void setAppIcon(String appIcon) {
		this.appIcon = appIcon;
	}

	public long getAppSize() {
		return appSize;
	}

	public void setAppSize(long appSize) {
		this.appSize = appSize;
	}

	public int getAppDownload() {
		return appDownload;
	}

	public void setAppDownload(int appDownload) {
		this.appDownload = appDownload;
	}

	public int getAppParse() {
		return appParse;
	}

	public void setAppParse(int appParse) {
		this.appParse = appParse;
	}

	public int getScriptParse() {
		return scriptParse;
	}

	public void setScriptParse(int scriptParse) {
		this.scriptParse = scriptParse;
	}

	public int getAppInstall() {
		return appInstall;
	}

	public void setAppInstall(int appInstall) {
		this.appInstall = appInstall;
	}

	public int getAppStart() {
		return appStart;
	}

	public void setAppStart(int appStart) {
		this.appStart = appStart;
	}

	public int getAppRun() {
		return appRun;
	}

	public void setAppRun(int appRun) {
		this.appRun = appRun;
	}

	public String getAppRunErrorDesc() {
		return appRunErrorDesc;
	}

	public void setAppRunErrorDesc(String appRunErrorDesc) {
		this.appRunErrorDesc = appRunErrorDesc;
	}

	public int getAppUninstall() {
		return appUninstall;
	}

	public void setAppUninstall(int appUninstall) {
		this.appUninstall = appUninstall;
	}

	public long getStartTime() {
		return startTime;
	}

	public void setStartTime(long startTime) {
		this.startTime = startTime;
	}

	public int getStartDuration() {
		return startDuration;
	}

	public void setStartDuration(int startDuration) {
		this.startDuration = startDuration;
	}

	public List<String> getCpuUsage() {
		return cpuUsage;
	}

	public void setCpuUsage(List<String> cpuUsage) {
		this.cpuUsage = cpuUsage;
	}

	public List<String> getMemoryUsage() {
		return memoryUsage;
	}

	public void setMemoryUsage(List<String> memoryUsage) {
		this.memoryUsage = memoryUsage;
	}

	public String getTrafficUp() {
		return trafficUp;
	}

	public void setTrafficUp(String trafficUp) {
		this.trafficUp = trafficUp;
	}

	public String getTrafficDown() {
		return trafficDown;
	}

	public void setTrafficDown(String trafficDown) {
		this.trafficDown = trafficDown;
	}

	public String getAppRunLog() {
		return appRunLog;
	}

	public void setAppRunLog(String appRunLog) {
		this.appRunLog = appRunLog;
	}

	public String getAppRunErrorLog() {
		return appRunErrorLog;
	}

	public void setAppRunErrorLog(String appRunErrorLog) {
		this.appRunErrorLog = appRunErrorLog;
	}

	public String getAppInstallLog() {
		return appInstallLog;
	}

	public void setAppInstallLog(String appInstallLog) {
		this.appInstallLog = appInstallLog;
	}

	public String getAppUninstallLog() {
		return appUninstallLog;
	}

	public void setAppUninstallLog(String appUninstallLog) {
		this.appUninstallLog = appUninstallLog;
	}

	public List<String> getScreenshots() {
		return screenshots;
	}

	public void setScreenshots(List<String> screenshots) {
		this.screenshots = screenshots;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	public String getTaskType() {
		return taskType;
	}

	public void setTaskType(String taskType) {
		this.taskType = taskType;
	}

	public List<String> getTest_time() {
		return test_time;
	}

	public void setTest_time(List<String> test_time) {
		this.test_time = test_time;
	}

	public String getInstall_error_reason() {
		return install_error_reason;
	}

	public void setInstall_error_reason(String install_error_reason) {
		this.install_error_reason = install_error_reason;
	}

	public String getStart_error_reason() {
		return start_error_reason;
	}

	public void setStart_error_reason(String start_error_reason) {
		this.start_error_reason = start_error_reason;
	}

	public String getUninstall_error_reason() {
		return uninstall_error_reason;
	}

	public void setUninstall_error_reason(String uninstall_error_reason) {
		this.uninstall_error_reason = uninstall_error_reason;
	}

	public int getInstallDuration() {
		return installDuration;
	}

	public void setInstallDuration(int installDuration) {
		this.installDuration = installDuration;
	}

	public List<String> getTemperature_arr() {
		return temperature_arr;
	}

	public void setTemperature_arr(List<String> temperature_arr) {
		this.temperature_arr = temperature_arr;
	}

	public List<String> getFps_arr() {
		return fps_arr;
	}

	public void setFps_arr(List<String> fps_arr) {
		this.fps_arr = fps_arr;
	}

	public List<String> getTraffic_up_usage() {
		return traffic_up_usage;
	}

	public void setTraffic_up_usage(List<String> traffic_up_usage) {
		this.traffic_up_usage = traffic_up_usage;
	}

	public List<String> getTraffic_down_usage() {
		return traffic_down_usage;
	}

	public void setTraffic_down_usage(List<String> traffic_down_usage) {
		this.traffic_down_usage = traffic_down_usage;
	}

	public boolean equals(TaskResult other) {

		if (equalsTwoString(this.appRunLog, other.appRunLog)
				&& equalsTwoString(this.appRunErrorLog, other.appRunErrorLog)
				&& equalsTwoString(this.install_error_reason,
						other.install_error_reason)
				&& equalsTwoString(this.uninstall_error_reason,
						other.uninstall_error_reason)
				&& equalsTwoString(this.start_error_reason,
						other.start_error_reason)
				&& isListEquals(this.cpuUsage, other.cpuUsage)) {
			return true;

		}
		return false;
	}

	/**
	 * 判断两个list是否相同
	 * 
	 * @param list1
	 * @param list2
	 * @return
	 */
	private Boolean isListEquals(List<String> list1, List<String> list2) {

		if (list1 == null && list2 == null) {
			return true;
		}

		if (list1 != null && list2 != null) {
			if (list1.size() != list2.size()) {
				return false;
			}
			for (int i = 0; i < list1.size(); i++) {
				if (!list1.get(i).equals(list2.get(i))) {
					return false;
				}
			}
		}
		return true;
	}

	private Boolean equalsTwoString(String str0, String str1) {
		if (str0 == null && str1 == null) {
			return true;
		}
		if (str0 == null && str1 != null) {
			return false;
		}
		if (str0 != null && str1 == null) {
			return false;
		}
		if (str0 != null && str1 != null) {
			return str0.equals(str1);
		}
		return false;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		return super.clone();

	}

	private String getNullSting2Not(String str) {

		if (str == null) {
			return "";
		} else
			return str;
	}

}
